package core;

public class Five_keyBoard extends Board {
    public Five_keyBoard(String player1Name, String player2Name) {
        super(player1Name, player2Name);
    }

    @Override
    protected void initializeBoard() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                grid[i][j] = EMPTY;
            }
        }
    }

    @Override
    public boolean makeMove(int row, int col) {
        if (gameOver || !isValidPosition(row, col) || grid[row][col] != EMPTY) {
            return false;
        }

        grid[row][col] = currentPlayer.getPiece();


        gameOver = isBoardFull()||isFive();


        if (!gameOver) {
            switchPlayer();
        }

        return true;
    }

    @Override
    public void display() {
        System.out.println("  A B C D E F G H");
        System.out.println("  -----------------");

        for (int i = 0; i < 8; i++) {
            System.out.print((i + 1) + " |");
            for (int j = 0; j < 8; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.print("|");

            // 显示玩家信息
            if (i == 2) {
                System.out.print("  游戏 " + (GameManager.getCurrentGameIndex() + 1));
            } else if (i == 3) {
                System.out.print("  " + player1.getName() +
                        (currentPlayer == player1 ? " " + BLACK : ""));
            } else if (i == 4) {
                System.out.print("  " + player2.getName() +
                        (currentPlayer == player2 ? " " + WHITE : ""));
            }

            System.out.println();
        }
        System.out.println("  -----------------");
        if (gameOver) {
            System.out.println("游戏结束，玩家"+currentPlayer.getName()+"获胜！");
        }
    }

    private boolean isBoardFull() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (grid[i][j] == EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }

    private boolean isFive(){
        char currentPiece = currentPlayer.getPiece();
        for (int row = 0; row < 8; row++) {
            for (int col = 0; col < 8; col++) {
                if (grid[row][col] != currentPiece) continue;
                for (int dx = -1; dx <= 1; dx++) {
                    for (int dy = -1; dy <= 1; dy++) {
                        if (dx == 0 && dy == 0) continue;
                        int count=1;
                        while (count<=5) {
                            if(count==5){
                                return true;
                            }
                            int row_imul_count=row+dx*count;
                            int col_imul_count=col+dy*count;
                            if(row_imul_count>=0 && col_imul_count>=0&&row_imul_count<8&&col_imul_count<8
                                    &&grid[row_imul_count][col_imul_count] == currentPiece) {
                                count++;
                            }
                            else break;
                        }
                    }
                }
            }
        }
        return false;
    }
}
